package com.corona.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.PixmapIO;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.corona.Game;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.corona.game.util.Save;
import com.corona.game.util.SmolSpriteProxy;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
import org.zeroturnaround.zip.ZipUtil;

/* loaded from: pokewilds.jar:com/pkmngen/game/PkmnMap.class */
public class PkmnMap {
    public Route currRoute;
    public Random rand;
    public String id;
    public static Color nightColor;
    public Pixmap minimap;
    public ArrayList<Tile> minimapQueue;
    public ShadeEffect shadeEffect;
    public String currMapId;
    public Rectangle boundingBox;
    private static final Vector2[] POSITIONS;
    public ArrayList<YSort> onscreenYsort1;
    public Rectangle viewBounds = new Rectangle();
    public Rectangle cacheBounds = new Rectangle();
    public boolean refreshCache = true;
    public boolean refreshOnscreenPokemon = false;
    public ArrayList<Tile> onscreenTiles = new ArrayList<>();
    public ArrayList<Tile> onscreenSpriteProxies = new ArrayList<>();
    public ArrayList<Tile> onscreenSprites = new ArrayList<>();
    public ArrayList<Tile> onscreenShoreTidal = new ArrayList<>();
    public ArrayList<Tile> onscreenShoreOcean = new ArrayList<>();
    public ArrayList<Tile> onscreenOverSprites = new ArrayList<>();
    public ArrayList<Tile> onscreenSavannaTrees = new ArrayList<>();
    public ArrayList<Tile> onscreenBelowBridgeTiles = new ArrayList<>();
    public ArrayList<Tile> onscreenDrawAsTree = new ArrayList<>();
    public ArrayList<Tile> onscreenGrass = new ArrayList<>();
    public ArrayList<Tile> onscreenExteriorTidal = new ArrayList<>();
    public ArrayList<Tile> onscreenFossils = new ArrayList<>();
    public ArrayList<Pokemon> onscreenPokemon = new ArrayList<>();
    public ArrayList<Pokemon> onscreenEggs = new ArrayList<>();
    public ArrayList<Pokemon> onscreenBurrowed = new ArrayList<>();
    public UpkeepTimers upkeepTimers = new UpkeepTimers();
    public Map<Vector2, Tile> overworldTiles = new HashMap();
    public Map<Vector2, Tile> tiles = this.overworldTiles;
    public ArrayList<HashMap<Vector2, Tile>> interiorTiles = new ArrayList<>();
    public int interiorTilesIndex = 100;
    public Map<Vector2, Pokemon> pokemon = new HashMap();
    public Map<Vector2, Pokemon> burrowedPokemon = new HashMap();
    public Vector2 bottomLeft = null;
    public Vector2 topRight = null;
    public ArrayList<Vector2> edges = new ArrayList<>();
    public String currBiome = "";
    public String timeOfDay = "day";
    public ArrayList<String> unownUsed = new ArrayList<>();
    private Vector2 tilePos = new Vector2();

    static {
        if (Game.staticGame.cinematic) {
            nightColor = new Color(0.3f, 0.3f, 0.7f, 1.0f);
        } else {
            nightColor = new Color(0.3f, 0.3f, 0.7f, 1.0f);
        }
        POSITIONS = new Vector2[]{new Vector2(-16.0f, 0.0f), new Vector2(16.0f, 0.0f), new Vector2(0.0f, -16.0f), new Vector2(0.0f, 16.0f), new Vector2(-16.0f, -16.0f), new Vector2(16.0f, 16.0f), new Vector2(16.0f, -16.0f), new Vector2(-16.0f, 16.0f)};
    }

    public Rectangle boundingBox() {
        if (this.boundingBox == null) {
            this.boundingBox = new Rectangle(this.bottomLeft.x + 16.0f, this.bottomLeft.y + 16.0f, (this.topRight.x - this.bottomLeft.x) - 32.0f, (this.topRight.y - this.bottomLeft.y) - 32.0f);
        }
        return this.boundingBox;
    }

    public PkmnMap(String mapName) {
        for (char c : "abcdefghijklmnopqrstuvwxyz".toCharArray()) {
            this.unownUsed.add(String.valueOf(c));
        }
        this.unownUsed.add("!");
        this.unownUsed.add("qmark");
        this.minimapQueue = new ArrayList<>();
        this.currMapId = "0,0";
        this.boundingBox = null;
        this.onscreenYsort1 = new ArrayList<>();
        this.id = mapName;
        this.rand = new Random();
        for (int i = 0; i < 100; i++) {
            this.interiorTiles.add(null);
        }
        this.interiorTiles.add(new HashMap<>());
        this.interiorTiles.add(new HashMap<>());
        this.currRoute = new Route("Demo_SteelRoute", 20);
    }

    public HashMap<Vector2, Tile> getInteriorLayer(int interiorIndex) {
        while (interiorIndex >= this.interiorTiles.size()) {
            this.interiorTiles.add(new HashMap<>());
        }
        HashMap<Vector2, Tile> interiorTiles = this.interiorTiles.get(interiorIndex);
        if (interiorTiles == null) {
            this.interiorTiles.remove(interiorIndex);
            interiorTiles = new HashMap<>();
            this.interiorTiles.add(interiorIndex, interiorTiles);
        }
        return interiorTiles;
    }

    public int getBuildingHeight(Tile startTile) {
        int numberOfWalls = 0;
        Tile currTile = startTile;
        boolean shouldStop = false;
        boolean foundRoof = false;
        while (!shouldStop) {
            if (currTile.isWall()) {
                numberOfWalls++;
            }
            if (currTile.nameUpper.contains("roof")) {
                foundRoof = true;
            }
            currTile = this.overworldTiles.get(currTile.position.cpy().add(0.0f, 16.0f));
            shouldStop = !currTile.nameUpper.contains("house");
            if (foundRoof) {
                shouldStop |= currTile.isWall();
            }
        }
        if (!foundRoof) {
            return -1;
        }
        Tile currTile2 = this.overworldTiles.get(startTile.position.cpy().add(0.0f, -16.0f));
        boolean shouldStop2 = !currTile2.nameUpper.contains("house");
        boolean foundWall = currTile2.isWall();
        if (foundWall) {
            shouldStop2 |= currTile2.nameUpper.contains("roof");
        }
        while (!shouldStop2) {
            if (currTile2.isWall()) {
                numberOfWalls++;
            }
            currTile2 = this.overworldTiles.get(currTile2.position.cpy().add(0.0f, -16.0f));
            shouldStop2 = !currTile2.nameUpper.contains("house");
            if (foundWall) {
                shouldStop2 |= currTile2.nameUpper.contains("roof");
            }
            if (!currTile2.nameUpper.contains("roof")) {
                foundWall = true;
            }
        }
        return numberOfWalls;
    }

    public void adjustSurroundingTiles(Tile currTile) {
        adjustSurroundingTiles(currTile, this.tiles);
    }

    public void adjustSurroundingTiles(Tile currTile, Map<Vector2, Tile> currTiles) {
        adjustSurroundingTiles(currTile, currTiles, currTiles == this.overworldTiles);
    }

    public void adjustSurroundingTiles(Tile currTile, Map<Vector2, Tile> currTiles, boolean isOverworld) {
        Vector2 pos = new Vector2();
        for (int i = -16; i < 17; i += 16) {
            for (int j = -16; j < 17; j += 16) {
                pos.set(currTile.position.cpy().add((float) i, (float) j));
                Tile tile = currTiles.get(pos);
                if (tile != null) {
                    adjustTile(tile, currTiles, isOverworld);
                    coastify(tile, currTiles, isOverworld);
                    waterfallify(tile, currTiles, isOverworld);
                    adjustPuddles(tile, currTiles, isOverworld);
                }
            }
        }
    }

    public void adjustTile(Tile currTile, Map<Vector2, Tile> currTiles) {
        adjustTile(currTile, currTiles, currTiles == this.overworldTiles);
    }

    public void waterfallify(Tile currTile, Map<Vector2, Tile> currTiles, boolean isOverworld) {
        if ((currTile.name.contains("water") || currTile.name.contains("lava")) && currTile.nameUpper.contains("ledges")) {
            String name = "waterfall1";
            if (currTile.name.contains("lava")) {
                name = "lavafall";
            }
            String[] tokens = currTile.nameUpper.split("_", 2);
            if (tokens.length >= 2) {
                currTile.name = name + ("_" + tokens[1]);
                currTile.init();
            }
        }
    }

    public void adjustPuddles(Tile currTile, Map<Vector2, Tile> currTiles, boolean isOverworld) {
        if (currTile.name.contains("_puddle1")) {
            String direction = "";
            String[] directions = {"W", "E", "S", "N", "SW", "NE", "SE", "NW"};
            Vector2[] positions = {new Vector2(-16.0f, 0.0f), new Vector2(16.0f, 0.0f), new Vector2(0.0f, -16.0f), new Vector2(0.0f, 16.0f), new Vector2(-16.0f, -16.0f), new Vector2(16.0f, 16.0f), new Vector2(16.0f, -16.0f), new Vector2(-16.0f, 16.0f)};
            int i = 0;
            while (i < positions.length && i < 4) {
                Tile nextTile = currTiles.get(currTile.position.cpy().add(positions[i]));
                if (nextTile != null && nextTile.name.contains("_puddle1")) {
                    direction = direction + "[" + directions[i] + "]";
                }
                i++;
            }
            currTile.name = currTile.name.split("_puddle1")[0] + "_puddle1" + direction;
            currTile.init();
        }
    }

    public boolean coastify(Tile currTile, Map<Vector2, Tile> currTiles, boolean isOverworld) {
        currTile.shoreOcean = null;
        currTile.shoreTidal = null;
        if (currTile.isWater || currTile.nameUpper.contains("ledges") || currTile.name.equals("ice2") || currTile.name.contains("bridge") || currTile.name.contains("waterfall")) {
            return false;
        }
        String direction = "";
        String directionTidal = "";
        String[] directions = {"W", "E", "S", "N", "SW", "NE", "SE", "NW"};
        for (int i = 0; i < POSITIONS.length; i++) {
            this.tilePos.set(currTile.position).add(POSITIONS[i]);
            Tile nextTile = currTiles.get(this.tilePos);
            if (nextTile != null && !nextTile.name.equals("water6")) {
                if (!currTile.isTidal && nextTile.isTidal) {
                    boolean found = false;
                    int k = 0;
                    while (true) {
                        if (directions[i].length() < 2 || k >= directions[i].length()) {
                            break;
                        } else if (directionTidal.contains("[" + directions[i].substring(k, k + 1) + "]")) {
                            found = true;
                            break;
                        } else {
                            k++;
                        }
                    }
                    if (!found) {
                        directionTidal = directionTidal + "[" + directions[i] + "]";
                    }
                } else if (nextTile.isWater) {
                    boolean found2 = false;
                    int k2 = 0;
                    while (true) {
                        if (directions[i].length() < 2 || k2 >= directions[i].length()) {
                            break;
                        } else if (direction.contains("[" + directions[i].substring(k2, k2 + 1) + "]")) {
                            found2 = true;
                            break;
                        } else {
                            k2++;
                        }
                    }
                    if (!found2) {
                        direction = direction + "[" + directions[i] + "]";
                        directionTidal = directionTidal + "[" + directions[i] + "]";
                    }
                }
            }
        }
        boolean coastified = false;
        if (!direction.equals("")) {
            currTile.shoreOcean = direction;
            coastified = true;
        }
        if (!directionTidal.equals("")) {
            currTile.shoreTidal = directionTidal;
            coastified = true;
        }
        currTile.init();
        return coastified;
    }

    public void ledgify(Tile tile) {
        Tile bl = this.tiles.get(tile.position.cpy().add(-16.0f, -16.0f));
        Tile down = this.tiles.get(tile.position.cpy().add(0.0f, -16.0f));
        Tile br = this.tiles.get(tile.position.cpy().add(16.0f, -16.0f));
        Tile left = this.tiles.get(tile.position.cpy().add(-16.0f, 0.0f));
        Tile tl = this.tiles.get(tile.position.cpy().add(-16.0f, 16.0f));
        Tile up = this.tiles.get(tile.position.cpy().add(0.0f, 16.0f));
        Tile tr = this.tiles.get(tile.position.cpy().add(16.0f, 16.0f));
        Tile right = this.tiles.get(tile.position.cpy().add(16.0f, 0.0f));
        if (bl == null) {
            bl = new Tile("black1", tile.position.cpy());
        }
        if (down == null) {
            down = new Tile("black1", tile.position.cpy());
        }
        if (br == null) {
            br = new Tile("black1", tile.position.cpy());
        }
        if (left == null) {
            left = new Tile("black1", tile.position.cpy());
        }
        if (tl == null) {
            tl = new Tile("black1", tile.position.cpy());
        }
        if (up == null) {
            up = new Tile("black1", tile.position.cpy());
        }
        if (tr == null) {
            tr = new Tile("black1", tile.position.cpy());
        }
        if (right == null) {
            right = new Tile("black1", tile.position.cpy());
        }
        String ledgeName = "ledges3";
        if (tile.nameUpper.contains("snow")) {
            ledgeName = "ledges3snow";
        } else if (tile.nameUpper.contains("volcano")) {
            ledgeName = "ledges3volcano";
        }
        if (left.nameUpper.contains("ledges3") && !down.nameUpper.contains("ledges3") && right.nameUpper.contains("ledges3")) {
            tile.nameUpper = ledgeName + "_N";
            tile.init();
        } else if (up.nameUpper.contains("ledges3") && right.nameUpper.contains("ledges3") && !down.nameUpper.contains("ledges3") && !left.nameUpper.contains("ledges3")) {
            tile.nameUpper = ledgeName + "_NE";
            tile.init();
        } else if (up.nameUpper.contains("ledges3") && !left.nameUpper.contains("ledges3") && down.nameUpper.contains("ledges3")) {
            tile.nameUpper = ledgeName + "_E";
            tile.init();
        } else if (!up.nameUpper.contains("ledges3") && !left.nameUpper.contains("ledges3") && right.nameUpper.contains("ledges3") && down.nameUpper.contains("ledges3")) {
            tile.nameUpper = ledgeName + "_SE";
            tile.init();
        } else if (!up.nameUpper.contains("ledges3") && left.nameUpper.contains("ledges3") && right.nameUpper.contains("ledges3")) {
            tile.nameUpper = ledgeName + "_S";
            tile.init();
        } else if (!up.nameUpper.contains("ledges3") && down.nameUpper.contains("ledges3") && left.nameUpper.contains("ledges3") && !right.nameUpper.contains("ledges3")) {
            tile.nameUpper = ledgeName + "_SW";
            tile.init();
        } else if (up.nameUpper.contains("ledges3") && down.nameUpper.contains("ledges3") && !right.nameUpper.contains("ledges3")) {
            tile.nameUpper = ledgeName + "_W";
            tile.init();
        } else if (up.nameUpper.contains("ledges3") && left.nameUpper.contains("ledges3") && !down.nameUpper.contains("ledges3") && !right.nameUpper.contains("ledges3")) {
            tile.nameUpper = ledgeName + "_NW";
            tile.init();
        } else if (!br.nameUpper.contains("ledges3") && down.nameUpper.contains("ledges3") && right.nameUpper.contains("ledges3")) {
            tile.nameUpper = ledgeName + "_NW_inner";
            tile.init();
        } else if (!bl.nameUpper.contains("ledges3") && down.nameUpper.contains("ledges3") && left.nameUpper.contains("ledges3")) {
            tile.nameUpper = ledgeName + "_NE_inner";
            tile.init();
        } else if (!tr.nameUpper.contains("ledges3") && up.nameUpper.contains("ledges3") && right.nameUpper.contains("ledges3")) {
            tile.nameUpper = ledgeName + "_SW_inner";
            tile.init();
        } else if (!tl.nameUpper.contains("ledges3") && up.nameUpper.contains("ledges3") && left.nameUpper.contains("ledges3")) {
            tile.nameUpper = ledgeName + "_SE_inner";
            tile.init();
        }
    }

    public void adjustTile(Tile currTile, Map<Vector2, Tile> currTiles, boolean isOverworld) {
        if (!currTile.name.contains("lava") || !currTile.nameUpper.equals("")) {
            Tile up = currTiles.get(currTile.position.cpy().add(0.0f, 16.0f));
            Tile down = currTiles.get(currTile.position.cpy().add(0.0f, -16.0f));
            Tile right = currTiles.get(currTile.position.cpy().add(16.0f, 0.0f));
            Tile left = currTiles.get(currTile.position.cpy().add(-16.0f, 0.0f));
            String[] tokens = currTile.nameUpper.split("_");
            String name = tokens[0];
            for (int i = 1; i < tokens.length - 1; i++) {
                name = name + "_" + tokens[i];
            }
            boolean touchUp = up != null && up.nameUpper.contains(name);
            boolean touchDown = down != null && down.nameUpper.contains(name);
            boolean touchRight = right != null && right.nameUpper.contains(name);
            boolean touchLeft = left != null && left.nameUpper.contains(name);
            String ext = "_";
            HashMap<Vector2, Tile> interiorTiles = this.interiorTiles.get(this.interiorTilesIndex);
            if (isOverworld) {
                if (currTile.nameUpper.contains("ledges3")) {
                    if (up != null && up.nameUpper.equals("ledges3_E") && right != null && right.nameUpper.equals("ledges3_N")) {
                        currTile.nameUpper = "ledges3_NE";
                        currTile.init();
                    } else if (up != null && up.nameUpper.equals("ledges3_W") && left != null && left.nameUpper.equals("ledges3_N")) {
                        currTile.nameUpper = "ledges3_N";
                        currTile.init();
                    } else if (down != null && down.nameUpper.equals("ledges3_W") && left != null && left.nameUpper.equals("ledges3_S")) {
                        currTile.nameUpper = "ledges3_SW";
                        currTile.init();
                    } else if (down != null && down.nameUpper.equals("ledges3_E") && right != null && right.nameUpper.equals("ledges3_S")) {
                        currTile.nameUpper = "ledges3_SE";
                        currTile.init();
                    }
                }
                if (currTile.nameUpper.contains("picture") || currTile.nameUpper.contains("house_plant") || currTile.nameUpper.contains("house_gym")) {
                    return;
                }
                if (currTile.nameUpper.contains("hole")) {
                    boolean touchUp2 = touchUp || (up != null && (up.name.contains("hole") || up.nameUpper.contains("hole")));
                    boolean touchDown2 = touchDown || (down != null && (down.name.contains("hole") || down.nameUpper.contains("hole")));
                    boolean touchRight2 = touchRight || (right != null && (right.name.contains("hole") || right.nameUpper.contains("hole")));
                    boolean touchLeft2 = touchLeft || (left != null && (left.name.contains("hole") || left.nameUpper.contains("hole")));
                    boolean touchUp3 = touchUp2 || (up != null && (up.name.contains("water") || up.nameUpper.contains("water")));
                    boolean touchDown3 = touchDown2 || (down != null && (down.name.contains("water") || down.nameUpper.contains("water")));
                    boolean touchRight3 = touchRight2 || (right != null && (right.name.contains("water") || right.nameUpper.contains("water")));
                    boolean touchLeft3 = touchLeft2 || (left != null && (left.name.contains("water") || left.nameUpper.contains("water")));
                    boolean touchUp4 = touchUp3 || (up != null && (up.name.contains("lava") || up.nameUpper.contains("lava")));
                    boolean touchDown4 = touchDown3 || (down != null && (down.name.contains("lava") || down.nameUpper.contains("lava")));
                    boolean touchRight4 = touchRight3 || (right != null && (right.name.contains("lava") || right.nameUpper.contains("lava")));
                    boolean touchLeft4 = touchLeft3 || (left != null && (left.name.contains("lava") || left.nameUpper.contains("lava")));
                    Tile NE = currTiles.get(currTile.position.cpy().add(16.0f, 16.0f));
                    Tile SE = currTiles.get(currTile.position.cpy().add(16.0f, -16.0f));
                    Tile SW = currTiles.get(currTile.position.cpy().add(-16.0f, -16.0f));
                    Tile NW = currTiles.get(currTile.position.cpy().add(-16.0f, 16.0f));
                    boolean touchNE = NE != null && (NE.name.contains("water") || NE.nameUpper.contains("water") || NE.nameUpper.contains("hole"));
                    boolean touchSE = SE != null && (SE.name.contains("water") || SE.nameUpper.contains("water") || SE.nameUpper.contains("hole"));
                    boolean touchSW = SW != null && (SW.name.contains("water") || SW.nameUpper.contains("water") || SW.nameUpper.contains("hole"));
                    boolean touchNW = NW != null && (NW.name.contains("water") || NW.nameUpper.contains("water") || NW.nameUpper.contains("hole"));
                    if (touchUp4) {
                        ext = ext + "N";
                    }
                    if (touchDown4) {
                        ext = ext + "S";
                    }
                    if (touchRight4) {
                        ext = ext + "E";
                    }
                    if (touchLeft4) {
                        ext = ext + "W";
                    }
                    if (ext.equals("_")) {
                        ext = "";
                    } else {
                        if (touchNE && touchUp4 && touchRight4) {
                            ext = ext + "[NE]";
                        }
                        if (touchSE && touchDown4 && touchRight4) {
                            ext = ext + "[SE]";
                        }
                        if (touchSW && touchDown4 && touchLeft4) {
                            ext = ext + "[SW]";
                        }
                        if (touchNW && touchUp4 && touchLeft4) {
                            ext = ext + "[NW]";
                        }
                    }
                    currTile.nameUpper = name + ext;
                    currTile.init();
                } else if (!currTile.nameUpper.contains("fence") || currTile.nameUpper.contains("house") || currTile.nameUpper.contains("gate")) {
                    if (currTile.nameUpper.contains("house") && !currTile.nameUpper.contains("roof") && !currTile.nameUpper.contains("door")) {
                        String window = "";
                        if (currTile.nameUpper.contains("exteriorwindows")) {
                            window = "exteriorwindows";
                        }
                        if (currTile.nameUpper.equals("house5_middle1") && down.nameUpper.contains("fence1") && !(touchUp ^ touchDown)) {
                            currTile.nameUpper = "house5_middle1_fence1" + window;
                            currTile.init();
                            if (!interiorTiles.containsKey(currTile.position)) {
                                interiorTiles.put(currTile.position.cpy(), new Tile("house5_floor1", currTile.position.cpy()));
                            }
                        } else if (currTile.nameUpper.contains("house6")) {
                            boolean touchUp5 = up != null && up.nameUpper.contains(name) && !up.nameUpper.contains("roof");
                            boolean touchDown5 = down != null && down.nameUpper.contains(name) && !down.nameUpper.contains("roof");
                            boolean touchRight5 = right != null && right.nameUpper.contains(name) && !right.nameUpper.contains("roof");
                            boolean touchLeft5 = left != null && left.nameUpper.contains(name) && !left.nameUpper.contains("roof");
                            if (touchUp5) {
                                ext = ext + "N";
                            }
                            if (touchDown5) {
                                ext = ext + "S";
                            }
                            if (touchRight5) {
                                ext = ext + "E";
                            }
                            if (touchLeft5) {
                                ext = ext + "W";
                            }
                            if (touchLeft5 && touchRight5 && !touchUp5 && !touchDown5) {
                                ext = "_NEW";
                            }
                            if (ext.length() <= 2) {
                                ext = ext + "N";
                            }
                            currTile.nameUpper = name + ext + window;
                            currTile.init();
                            if (!interiorTiles.containsKey(currTile.position)) {
                                interiorTiles.put(currTile.position.cpy(), new Tile("house5_floor1", currTile.position.cpy()));
                            }
                        } else if (currTile.nameUpper.contains("house7")) {
                            if (touchUp) {
                                ext = ext + "N";
                            }
                            if (touchDown) {
                                ext = ext + "S";
                            }
                            if (touchRight) {
                                ext = ext + "E";
                            }
                            if (touchLeft) {
                                ext = ext + "W";
                            }
                            if (ext.length() < 2) {
                                ext = ext + "S";
                            }
                            currTile.nameUpper = name + ext + window;
                            currTile.init();
                            if (!interiorTiles.containsKey(currTile.position)) {
                                interiorTiles.put(currTile.position.cpy(), new Tile("house5_floor1", currTile.position.cpy()));
                            }
                        } else if (currTile.nameUpper.contains("house8")) {
                            boolean touchUp6 = up != null && up.nameUpper.contains(name) && !up.nameUpper.contains("roof");
                            boolean touchDown6 = down != null && down.nameUpper.contains(name) && !down.nameUpper.contains("roof");
                            boolean touchRight6 = right != null && right.nameUpper.contains(name) && !right.nameUpper.contains("roof");
                            boolean touchLeft6 = left != null && left.nameUpper.contains(name) && !left.nameUpper.contains("roof");
                            if (touchUp6) {
                                ext = ext + "N";
                            }
                            if (touchDown6) {
                                ext = ext + "S";
                            }
                            if (touchRight6) {
                                ext = ext + "E";
                            }
                            if (touchLeft6) {
                                ext = ext + "W";
                            }
                            if (touchLeft6 && touchRight6 && !touchUp6 && !touchDown6) {
                                ext = "_NEW";
                            }
                            if (ext.length() <= 2) {
                                ext = ext + "N";
                            }
                            currTile.nameUpper = name + ext + window;
                            currTile.init();
                            if (!interiorTiles.containsKey(currTile.position)) {
                                interiorTiles.put(currTile.position.cpy(), new Tile("house5_floor1", currTile.position.cpy()));
                            }
                        } else if (currTile.nameUpper.contains("house9")) {
                            boolean touchUp7 = up != null && up.nameUpper.contains(name) && !up.nameUpper.contains("roof");
                            boolean touchDown7 = down != null && down.nameUpper.contains(name) && !down.nameUpper.contains("roof");
                            boolean touchRight7 = right != null && right.nameUpper.contains(name);
                            boolean touchLeft7 = left != null && left.nameUpper.contains(name);
                            if (touchUp7) {
                                ext = ext + "N";
                            }
                            if (touchDown7) {
                                ext = ext + "S";
                            }
                            if (touchRight7) {
                                ext = ext + "E";
                            }
                            if (touchLeft7) {
                                ext = ext + "W";
                            }
                            if (touchLeft7 && touchRight7 && !touchUp7 && !touchDown7) {
                                ext = "_NEW";
                            }
                            if (ext.length() <= 2) {
                                ext = ext + "N";
                            }
                            currTile.nameUpper = name + ext + window;
                            currTile.init();
                            if (!interiorTiles.containsKey(currTile.position)) {
                                interiorTiles.put(currTile.position.cpy(), new Tile("house5_floor1", currTile.position.cpy()));
                            }
                        } else if (touchLeft || touchRight) {
                            if (touchRight && touchLeft) {
                                ext = ext + "middle1";
                            } else if (touchRight && touchDown) {
                                ext = ext + "E";
                            } else if (touchLeft && touchDown) {
                                ext = ext + "W";
                            } else if (touchRight) {
                                ext = ext + "left1";
                            } else if (touchLeft) {
                                ext = ext + "right1";
                            }
                            currTile.nameUpper = name + ext + window;
                            currTile.init();
                            if (!interiorTiles.containsKey(currTile.position)) {
                                interiorTiles.put(currTile.position.cpy(), new Tile("house5_floor1", currTile.position.cpy()));
                            }
                        }
                    } else if (currTile.nameUpper.contains("house") && currTile.nameUpper.contains("roof")) {
                        String chimney = "";
                        if (currTile.nameUpper.contains("chimney")) {
                            chimney = "chimney";
                        }
                        if (touchUp || touchDown || touchLeft || touchRight) {
                            if (touchUp) {
                                ext = ext + "N";
                            }
                            if (touchDown) {
                                ext = ext + "S";
                            }
                            if (touchRight) {
                                ext = ext + "E";
                            }
                            if (touchLeft) {
                                ext = ext + "W";
                            }
                            if (touchLeft && touchRight && !touchUp && !touchDown) {
                                ext = "_middle1";
                            }
                            if ((touchUp || touchDown) && !touchLeft && !touchRight) {
                                ext = "_NSEW";
                            }
                            if (ext.length() == 2) {
                                ext = "";
                                if (touchLeft) {
                                    ext = "_right1";
                                } else if (touchRight) {
                                    ext = "_left1";
                                }
                            }
                            if (currTile.nameUpper.contains("house6") && ext.length() > 3) {
                                Tile NE2 = currTiles.get(currTile.position.cpy().add(16.0f, 16.0f));
                                Tile SE2 = currTiles.get(currTile.position.cpy().add(16.0f, -16.0f));
                                Tile SW2 = currTiles.get(currTile.position.cpy().add(-16.0f, -16.0f));
                                Tile NW2 = currTiles.get(currTile.position.cpy().add(-16.0f, 16.0f));
                                boolean touchNE2 = NE2 != null && NE2.nameUpper.contains("roof");
                                boolean touchSE2 = SE2 != null && SE2.nameUpper.contains("roof");
                                boolean touchSW2 = SW2 != null && SW2.nameUpper.contains("roof");
                                boolean touchNW2 = NW2 != null && NW2.nameUpper.contains("roof");
                                if (!touchSE2 && touchDown && touchRight) {
                                    ext = "_SEinner";
                                } else if (!touchSW2 && touchDown && touchLeft) {
                                    ext = "_SWinner";
                                } else if (!touchNE2 && touchUp && touchRight) {
                                    ext = "_NEinner";
                                } else if (!touchNW2 && touchUp && touchLeft) {
                                    ext = "_NWinner";
                                }
                            }
                            if (currTile.nameUpper.contains("house7")) {
                                ext = "_";
                                if (touchUp) {
                                    ext = ext + "N";
                                }
                                if (touchDown) {
                                    ext = ext + "S";
                                }
                                if (touchRight) {
                                    ext = ext + "E";
                                }
                                if (touchLeft) {
                                    ext = ext + "W";
                                }
                            }
                            currTile.nameUpper = name + ext + chimney;
                            currTile.init();
                            if (!interiorTiles.containsKey(currTile.position) || interiorTiles.get(currTile.position).name.contains("wall")) {
                                interiorTiles.put(currTile.position.cpy(), new Tile("house5_floor1", currTile.position.cpy()));
                            }
                            if (!interiorTiles.containsKey(up.position) || interiorTiles.get(up.position) == null) {
                                interiorTiles.put(up.position.cpy(), new Tile("house5_wall1", up.position.cpy()));
                            }
                        }
                    } else if (currTile.nameUpper.contains("door")) {
                        if (!(right == null || left == null)) {
                            if ((left.nameUpper.contains("roof") || left.nameUpper.contains("door")) && ((right.nameUpper.contains("roof") || right.nameUpper.contains("door")) && (!left.nameUpper.contains("door") || !right.nameUpper.contains("door")))) {
                                String name2 = "house5_roof_middle1";
                                if (currTile.nameUpper.contains("house6")) {
                                    name2 = "house6_roof_middle1";
                                } else if (currTile.nameUpper.contains("house7")) {
                                    name2 = "house7_roof_S";
                                } else if (currTile.nameUpper.contains("house8")) {
                                    name2 = "house8_roof_S";
                                } else if (currTile.nameUpper.contains("house9")) {
                                    name2 = "house9_roof_S";
                                }
                                currTile.nameUpper = name2;
                                currTile.init();
                                adjustSurroundingTiles(currTile, currTiles);
                                if (!up.isSolid) {
                                    currTiles.put(up.position.cpy(), new Tile("rug2", "", up.position.cpy(), true, up.routeBelongsTo));
                                    if (!interiorTiles.containsKey(up.position) || interiorTiles.get(up.position).name.contains("wall")) {
                                        interiorTiles.put(up.position.cpy(), new Tile("house5_door1", up.position.cpy()));
                                    }
                                }
                                if (interiorTiles.containsKey(currTile.position) && interiorTiles.get(currTile.position).name.contains("rug")) {
                                    interiorTiles.put(currTile.position.cpy(), new Tile("house5_floor1", currTile.position.cpy()));
                                    return;
                                }
                                return;
                            } else if ((left.nameUpper.contains("fence") || left.nameUpper.contains("door") || left.nameUpper.contains("wall") || left.nameUpper.contains("right") || left.nameUpper.contains("W")) && ((right.nameUpper.contains("fence") || right.nameUpper.contains("door") || right.nameUpper.contains("wall") || right.nameUpper.contains("left") || right.nameUpper.contains("E")) && (!left.isWall2() || !right.isWall2()))) {
                                String fenceName = "fence1";
                                if (name.contains("house6") || name.contains("house8")) {
                                    fenceName = "fence2";
                                }
                                if (currTile.nameUpper.contains("door")) {
                                    interiorTiles.remove(currTile.position.cpy());
                                }
                                currTile.nameUpper = fenceName + "gate1";
                                currTile.init();
                                adjustSurroundingTiles(currTile, currTiles);
                                return;
                            }
                        }
                        if (!(up == null || down == null)) {
                            if (!up.nameUpper.contains("fence") && !down.nameUpper.contains("fence") && ((up.nameUpper.contains("roof") || up.nameUpper.contains("E") || up.nameUpper.contains("W") || up.nameUpper.contains("left") || up.nameUpper.contains("right") || up.nameUpper.contains("door")) && ((down.nameUpper.contains("roof") || down.nameUpper.contains("E") || down.nameUpper.contains("W") || down.nameUpper.contains("left") || down.nameUpper.contains("right") || down.nameUpper.contains("door")) && (!up.nameUpper.contains("door") || !down.nameUpper.contains("door"))))) {
                                int roofCount = 0;
                                if (right != null && right.nameUpper.contains("roof")) {
                                    roofCount = 0 + 1;
                                }
                                if (left != null && left.nameUpper.contains("roof")) {
                                    roofCount++;
                                }
                                String name22 = "house5_middle1";
                                if (roofCount > 0) {
                                    name22 = "house5_roof_middle1";
                                }
                                if (name.contains("house6")) {
                                    name22 = "house6_NS";
                                    if (roofCount > 0) {
                                        name22 = "house6_roof_NS";
                                    }
                                } else if (name.contains("house7")) {
                                    name22 = "house7_wall_NS";
                                    if (roofCount > 0) {
                                        name22 = "house7_roof_NS";
                                    }
                                } else if (name.contains("house8")) {
                                    name22 = "house8_NS";
                                    if (roofCount > 0) {
                                        name22 = "house8_roof_NS";
                                    }
                                } else if (name.contains("house9")) {
                                    name22 = "house9_NS";
                                    if (roofCount > 0) {
                                        name22 = "house9_roof_NS";
                                    }
                                }
                                currTile.nameUpper = name22;
                                currTile.init();
                                adjustSurroundingTiles(currTile);
                                if (!left.isSolid) {
                                    this.tiles.put(left.position.cpy(), new Tile("rug2_right", "", left.position.cpy(), true, left.routeBelongsTo));
                                    Tile interiorTile = interiorTiles.get(currTile.position);
                                    if (interiorTile == null || interiorTile.name.contains("floor")) {
                                        interiorTiles.put(currTile.position.cpy(), new Tile("rug2_left", currTile.position.cpy()));
                                        return;
                                    }
                                    return;
                                } else if (!right.isSolid) {
                                    this.tiles.put(right.position.cpy(), new Tile("rug2_left", "", right.position.cpy(), true, right.routeBelongsTo));
                                    Tile interiorTile2 = interiorTiles.get(currTile.position);
                                    if (interiorTile2 == null || interiorTile2.name.contains("floor")) {
                                        interiorTiles.put(currTile.position.cpy(), new Tile("rug2_right", currTile.position.cpy()));
                                        return;
                                    }
                                    return;
                                } else {
                                    return;
                                }
                            } else if (!up.nameUpper.contains("roof") && ((!up.nameUpper.contains("house") || !up.nameUpper.contains("S")) && ((up.nameUpper.contains("fence") || up.nameUpper.contains("gate") || up.nameUpper.contains("wall") || up.nameUpper.contains("left") || up.nameUpper.contains("right") || up.nameUpper.contains("W") || up.nameUpper.contains("E") || up.nameUpper.contains("door")) && (down.nameUpper.contains("fence") || down.nameUpper.contains("gate") || down.nameUpper.contains("wall") || down.nameUpper.contains("left") || down.nameUpper.contains("right") || down.nameUpper.contains("W") || down.nameUpper.contains("E") || down.nameUpper.contains("door"))))) {
                                String fenceName2 = "fence1";
                                if (name.contains("house6") || name.contains("house8")) {
                                    fenceName2 = "fence2";
                                }
                                if (currTile.nameUpper.contains("door")) {
                                    interiorTiles.remove(currTile.position.cpy());
                                }
                                currTile.nameUpper = fenceName2 + "gate1_NS";
                                currTile.init();
                                adjustSurroundingTiles(currTile, currTiles);
                                return;
                            }
                        }
                        if (!interiorTiles.containsKey(currTile.position)) {
                            interiorTiles.put(currTile.position.cpy(), new Tile("house5_floor_rug1", currTile.position.cpy()));
                        }
                    }
                } else if (up != null && down != null && right != null && left != null) {
                    if (down.nameUpper.contains("house")) {
                        touchDown = true;
                    }
                    if (left.nameUpper.contains("house") && !left.nameUpper.contains("roof")) {
                        touchLeft = true;
                    }
                    if (right.nameUpper.contains("house") && !right.nameUpper.contains("roof")) {
                        touchRight = true;
                    }
                    if (!currTile.nameUpper.contains("fence2") && up.nameUpper.contains("house")) {
                        touchUp = true;
                    }
                    if (touchUp || touchDown || touchLeft || touchRight) {
                        if (touchUp) {
                            ext = ext + "N";
                        }
                        if (touchDown) {
                            ext = ext + "S";
                        }
                        if (touchRight) {
                            ext = ext + "E";
                        }
                        if (touchLeft) {
                            ext = ext + "W";
                        }
                        if (touchLeft && touchRight && !touchUp && !touchDown) {
                            ext = "";
                        }
                        if ((touchUp || touchDown) && !touchLeft && !touchRight) {
                            ext = "_NS";
                        }
                        if (ext.length() == 2) {
                            ext = "";
                        }
                        currTile.nameUpper = name + ext;
                        currTile.init();
                    }
                }
            } else if (currTile.nameUpper.contains("table")) {
                if (touchUp || touchDown || touchLeft || touchRight) {
                    if (touchUp) {
                        ext = ext + "N";
                    }
                    if (touchDown) {
                        ext = ext + "S";
                    }
                    if (touchRight) {
                        ext = ext + "E";
                    }
                    if (touchLeft) {
                        ext = ext + "W";
                    }
                    if (ext.equals("_E")) {
                        ext = "_left1";
                    } else if (ext.equals("_W")) {
                        ext = "_right1";
                    } else if (ext.equals("_EW")) {
                        ext = "_middle1";
                    } else if (ext.equals("_NS")) {
                        ext = "_middle2";
                    } else if (ext.equals("_N")) {
                        ext = "_down1";
                    } else if (ext.equals("_S")) {
                        ext = "_up1";
                    }
                    currTile.nameUpper = name + ext;
                    currTile.init();
                }
            } else if (currTile.nameUpper.contains("interiorwall")) {
                if (touchUp || touchDown || touchLeft || touchRight) {
                    if (touchUp || up.name.contains("wall")) {
                        ext = ext + "N";
                    }
                    if (touchDown) {
                        ext = ext + "S";
                    }
                    if (touchRight || right == null) {
                        ext = ext + "E";
                    }
                    if (touchLeft || left == null) {
                        ext = ext + "W";
                    }
                    currTile.nameUpper = name + ext;
                    currTile.init();
                }
            } else if (currTile.name.contains("ruins1") && !currTile.name.contains("pillar")) {
                String[] tokens2 = currTile.name.split("_");
                String name3 = tokens2[0];
                for (int i2 = 1; i2 < tokens2.length - 1; i2++) {
                    name3 = name3 + "_" + tokens2[i2];
                }
                boolean touchUp8 = up != null && up.name.contains(name3);
                boolean touchDown8 = down != null && down.name.contains(name3);
                boolean touchRight8 = right != null && right.name.contains(name3);
                boolean touchLeft8 = left != null && left.name.contains(name3);
                if (touchUp8) {
                    ext = ext + "N";
                }
                if (touchDown8) {
                    ext = ext + "S";
                }
                if (touchRight8) {
                    ext = ext + "E";
                }
                if (touchLeft8) {
                    ext = ext + "W";
                }
                currTile.name = name3 + ext;
                currTile.init();
            }
        } else {
            String ext2 = "_";
            String[] cols = {"S", "", "N"};
            String[] rows = {"W", "", "E"};
            if (Game.rand.nextInt(3) != 0) {
                if (Game.rand.nextBoolean()) {
                    ext2 = (ext2 + cols[Game.rand.nextInt(3)]) + rows[Math.floorMod((int) currTile.position.x, 48) / 16];
                } else {
                    ext2 = (ext2 + cols[Math.floorMod((int) currTile.position.y, 48) / 16]) + rows[Game.rand.nextInt(3)];
                }
            }
            currTile.name = "lava1" + ext2;
            currTile.init();
        }
    }

    /* JADX DEBUG: Multi-variable search result rejected for r0v242, resolved type: java.util.ArrayList<java.util.HashMap<com.badlogic.gdx.math.Vector2, com.corona.game.Tile>> */
    /* JADX WARN: Multi-variable type inference failed */
    public void loadFromFileLegacy(Game game) {
        Tile down;
        try {
            Input input = new Input(new InflaterInputStream(new FileInputStream(this.id + ".sav")));
            Network.SaveData saveData = (Network.SaveData) game.server.getKryo().readObject(input, Network.SaveData.class);
            input.close();
            this.tiles.clear();
            this.bottomLeft = new Vector2();
            this.topRight = new Vector2();
            HashMap<String, Route> loadedRoutes = new HashMap<>();
            Iterator<Network.TileData> it = saveData.mapTiles.tiles.iterator();
            while (it.hasNext()) {
                Network.TileDataBase tileData = it.next();
                if (tileData.routeBelongsTo != null && !loadedRoutes.containsKey(tileData.routeBelongsTo)) {
                    loadedRoutes.put(tileData.routeBelongsTo, new Route(saveData.mapTiles.routes.get(tileData.routeBelongsTo)));
                }
                Tile newTile = Tile.get(tileData, loadedRoutes.get(tileData.routeBelongsTo));
                this.tiles.put(tileData.pos.cpy(), newTile);
                if (newTile.items != null && newTile.items.containsKey("trapinch")) {
                    Pokemon trapinch = new Pokemon("trapinch", 22, Pokemon.Generation.CRYSTAL);
                    trapinch.isTrapping = true;
                    trapinch.position = newTile.position.cpy();
                    trapinch.mapTiles = game.map.tiles;
                    Objects.requireNonNull(trapinch);
                    game.insertAction(new Pokemon.Burrowed());
                }
                if (this.topRight.x < tileData.pos.x) {
                    this.topRight.x = tileData.pos.x;
                }
                if (this.topRight.y < tileData.pos.y) {
                    this.topRight.y = tileData.pos.y;
                }
                if (this.bottomLeft.x > tileData.pos.x) {
                    this.bottomLeft.x = tileData.pos.x;
                }
                if (this.bottomLeft.y > tileData.pos.y) {
                    this.bottomLeft.y = tileData.pos.y;
                }
            }
            this.interiorTiles.clear();
            Iterator<HashMap<Vector2, Network.TileData>> it2 = saveData.mapTiles.interiorTiles.iterator();
            while (it2.hasNext()) {
                HashMap<Vector2, Network.TileData> tileDatas = it2.next();
                HashMap<Vector2, Tile> tiles = null;
                if (tileDatas != null) {
                    tiles = new HashMap<>();
                    for (Network.TileDataBase tileData2 : tileDatas.values()) {
                        if (tileData2.routeBelongsTo == null && tileData2.tileName.contains("pkmnmansion") && !tileData2.tileName.contains("stairs")) {
                            tileData2.routeBelongsTo = "pkmnmansion_temp";
                            if (!loadedRoutes.containsKey(tileData2.routeBelongsTo)) {
                                loadedRoutes.put(tileData2.routeBelongsTo, new Route("pkmnmansion1", 30));
                            }
                        }
                        if (tileData2.routeBelongsTo != null && !loadedRoutes.containsKey(tileData2.routeBelongsTo)) {
                            loadedRoutes.put(tileData2.routeBelongsTo, new Route(saveData.mapTiles.routes.get(tileData2.routeBelongsTo)));
                        }
                        Tile newTile2 = Tile.get(tileData2, loadedRoutes.get(tileData2.routeBelongsTo));
                        tiles.put(newTile2.position.cpy(), newTile2);
                    }
                }
                this.interiorTiles.add(tiles);
            }
            this.interiorTilesIndex = saveData.mapTiles.interiorTilesIndex;
            this.timeOfDay = saveData.mapTiles.timeOfDay;
            CycleDayNight.dayTimer = saveData.mapTiles.dayTimer;
            this.edges = saveData.mapTiles.edges;
            Iterator<Network.PlayerDataBase> it3 = saveData.players.iterator();
            while (it3.hasNext()) {
                Network.PlayerDataBase playerData = it3.next();
                Player player = new Player(playerData);
                player.type = Player.Type.REMOTE;
                game.players.put(playerData.id, player);
            }
            game.player = new Player(saveData.playerData);
            Iterator<Pokemon> it4 = game.player.pokemon.iterator();
            while (it4.hasNext()) {
                it4.next().previousOwner = game.player;
            }
            game.cam.position.set(game.player.position.x + 16.0f, game.player.position.y, 0.0f);
            game.player.type = Player.Type.LOCAL;
            if (saveData.playerData.isInterior) {
                game.map.tiles = game.map.interiorTiles.get(game.map.interiorTilesIndex);
            }
            if (game.player.currFieldMove.equals("FLY")) {
                PlayerStanding standingAction = null;
                Iterator<Action> it5 = game.actionStack.iterator();
                while (true) {
                    if (!it5.hasNext()) {
                        break;
                    }
                    Action action = it5.next();
                    if (PlayerStanding.class.isInstance(action)) {
                        standingAction = (PlayerStanding) action;
                        break;
                    }
                }
                game.actionStack.remove(standingAction);
                Player player2 = game.player;
                Objects.requireNonNull(player2);
                game.insertAction(new Player.Flying(game.player.pokemon.get(saveData.playerData.flyingIndex), false, null));
                game.cam.translate(0.0f, 16.0f, 0.0f);
            }
            game.map.pokemon.clear();
            for (Vector2 pos : saveData.overworldPokemon.keySet()) {
                Pokemon pokemon = new Pokemon(saveData.overworldPokemon.get(pos));
                pokemon.position = pos.cpy();
                game.map.pokemon.put(pos, pokemon);
                Objects.requireNonNull(pokemon);
                game.insertAction(new Pokemon.Standing());
            }
            float startTime = (float) System.currentTimeMillis();
            for (Tile tile : game.map.overworldTiles.values()) {
                game.map.coastify(tile, game.map.overworldTiles, true);
                if (tile.name.contains("_lower")) {
                    System.out.println("tile.name");
                    System.out.println(tile.name);
                    tile.name = tile.name.split("_")[1];
                    tile.init();
                }
                if (tile.name.equals("bridge1_water2") || tile.name.equals("bridge1_water5")) {
                    String[] vals = tile.name.split("_");
                    tile.name = vals[0];
                    tile.squish(vals[1]);
                }
            }
            for (Tile tile2 : game.map.overworldTiles.values()) {
                if (tile2.name.contains("bridge") && (down = this.overworldTiles.get(tile2.position.cpy().add(0.0f, -16.0f))) != null && (down.name.contains("water") || down.name.contains("_tidal") || down.name.contains("lava"))) {
                    down.belowBridge = true;
                    down.isSolid = true;
                }
            }
            System.out.println("End coastify: " + String.valueOf(((float) System.currentTimeMillis()) - startTime));
            FileHandle file = Gdx.files.local(this.id + ".png");
            if (file.exists()) {
                this.minimap = new Pixmap(file);
            } else {
                Game.staticGame.map.minimap = new Pixmap(((int) (Game.staticGame.map.topRight.x - Game.staticGame.map.bottomLeft.x)) / 8, ((int) (Game.staticGame.map.topRight.y - Game.staticGame.map.bottomLeft.y)) / 8, Pixmap.Format.RGBA8888);
                Game.staticGame.map.minimap.setColor(0.0f, 0.0f, 0.0f, 1.0f);
                Game.staticGame.map.minimap.fill();
                Vector2 startPos = game.player.position.cpy().add(-128.0f, -128.0f);
                startPos.x = (float) (((int) startPos.x) - (((int) startPos.x) % 16));
                startPos.y = (float) (((int) startPos.y) - (((int) startPos.y) % 16));
                Vector2 endPos = game.player.position.cpy().add(128.0f, 128.0f);
                endPos.x = (float) (((int) endPos.x) - (((int) endPos.x) % 16));
                endPos.y = (float) (((int) endPos.y) - (((int) endPos.y) % 16));
                Vector2 currPos = new Vector2(startPos.x, startPos.y);
                while (currPos.y < endPos.y) {
                    Tile tile3 = Game.staticGame.map.tiles.get(currPos);
                    currPos.x += 16.0f;
                    if (currPos.x > endPos.x) {
                        currPos.x = startPos.x;
                        currPos.y += 16.0f;
                    }
                    if (tile3 != null) {
                        tile3.updateMiniMap(game);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("No save file found for map: " + this.id);
        }
    }

    public void saveToFileNew(Game game) {
        PeriodicSave.resetTimer();
        System.out.println("Backing up previous save file...");
        try {
            ZipUtil.pack(new File(game.map.id + ".sav/"), new File(game.map.id + ".sav.zip"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Saving game to file...");
        FileHandle directory = Gdx.files.local(this.id + ".sav/");
        if (directory.exists() && !directory.isDirectory()) {
            directory.moveTo(Gdx.files.local(this.id + ".sav.old"));
        }
        if (!directory.exists()) {
            directory.mkdirs();
        }
        try {
            System.out.println("Writing game data...");
            Save.saveData(new Network.GameSaveData(game), this.id + ".sav/game");
            System.out.println("Writing map data...");
            Save.saveData(new Network.MapSaveData(game), this.id + ".sav/map" + this.currMapId + "");
            System.out.println("Writing player spawn data...");
            Save.saveData(new Network.PlayerSpawnLocationData(game.player), this.id + ".sav/spawn" + game.player.network.id + this.currMapId + "");
            System.out.println("Writing minimap data...");
            Save.saveMinimap(this, this.id + ".sav/map" + this.currMapId + ".png");
        } catch (Save.SaveFailure e2) {
            Game.saveErrorLogAndNotifyUser("An issue occurred while saving:\n\n", e2);
        }
        System.out.println("Done.");
    }

    public int height() {
        return (int) (this.topRight.y - this.bottomLeft.y);
    }

    public int width() {
        return (int) (this.topRight.x - this.bottomLeft.x);
    }

    /*  JADX ERROR: JadxRuntimeException in pass: BlockProcessor
        jadx.core.utils.exceptions.JadxRuntimeException: Unreachable block: B:96:0x0582
        	at jadx.core.dex.visitors.blocks.BlockProcessor.checkForUnreachableBlocks(BlockProcessor.java:86)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.processBlocksTree(BlockProcessor.java:52)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.visit(BlockProcessor.java:44)
        */
    public void loadMapFromFile(Game r8) {
        /*
        // Method dump skipped, instructions count: 1825
        */
        throw new UnsupportedOperationException("Method not decompiled: com.corona.game.PkmnMap.loadMapFromFile(com.corona.Game):void");
    }

    public void loadFromFile(Game game) {
        try {
            if (!Gdx.files.local(game.map.id + ".sav").isDirectory()) {
                System.out.println("Using legacy loading");
                loadFromFileLegacy(game);
                game.alreadyUsedDungeons.add("mansion1");
                game.alreadyUsedDungeons.add("regi1");
                game.alreadyUsedDungeons.add("desert_ruins1");
                return;
            }
            Network.GameSaveData gameData = (Network.GameSaveData) Save.readData(game.map.id + ".sav/game", Network.GameSaveData.class);
            Iterator<Network.PlayerData> it = gameData.players.iterator();
            while (it.hasNext()) {
                Network.PlayerDataBase playerData = it.next();
                Player player = new Player(playerData);
                player.type = Player.Type.REMOTE;
                game.players.put(playerData.id, player);
            }
            if (Float.isNaN(gameData.playerData.position.x) || Float.isNaN(gameData.playerData.position.y)) {
                gameData.playerData.position.x = 0.0f;
                gameData.playerData.position.y = 0.0f;
            }
            game.player = new Player(gameData.playerData);
            Iterator<Pokemon> it2 = game.player.pokemon.iterator();
            while (it2.hasNext()) {
                it2.next().previousOwner = game.player;
            }
            game.cam.position.set(game.player.position.x + 16.0f, game.player.position.y, 0.0f);
            game.player.type = Player.Type.LOCAL;
            if (game.player.hmPokemon != null) {
                String currFieldMove = game.player.currFieldMove;
                if (currFieldMove.equals("FLY")) {
                    PlayerStanding standingAction = null;
                    Iterator<Action> it3 = game.actionStack.iterator();
                    while (true) {
                        if (!it3.hasNext()) {
                            break;
                        }
                        Action action = it3.next();
                        if (PlayerStanding.class.isInstance(action)) {
                            standingAction = (PlayerStanding) action;
                            break;
                        }
                    }
                    game.actionStack.remove(standingAction);
                    Player player2 = game.player;
                    Objects.requireNonNull(player2);
                    game.insertAction(new Player.Flying(game.player.hmPokemon, false, null));
                    game.cam.translate(0.0f, 16.0f, 0.0f);
                } else {
                    if (!currFieldMove.equals("")) {
                        game.player.swapSprites(game.player.hmPokemon);
                    }
                    if (!currFieldMove.equals("RIDE") && !currFieldMove.equals("SURF")) {
                        Pokemon pokemon = game.player.hmPokemon;
                        Objects.requireNonNull(pokemon);
                        game.insertAction(new Pokemon.Follow(game.player));
                    }
                }
            }
            this.timeOfDay = gameData.timeOfDay;
            CycleDayNight.dayTimer = gameData.dayTimer;
            this.currMapId = gameData.currMapId;
            game.alreadyUsedDungeons = gameData.alreadyUsedDungeons;
            System.out.println("used dungeons");
            Iterator<String> it4 = game.alreadyUsedDungeons.iterator();
            while (it4.hasNext()) {
                System.out.println(it4.next());
            }
            loadMapFromFile(game);
            if (gameData.playerData.isInterior) {
                game.map.tiles = game.map.interiorTiles.get(game.map.interiorTilesIndex);
            }
        } catch (GdxRuntimeException | Save.ReadFailure e) {
            System.out.println("No save file found for map: " + this.id + this.currMapId);
        }
    }

    /* loaded from: pokewilds.jar:com/pkmngen/game/PkmnMap$ShadeEffect.class */
    public class ShadeEffect extends Action {
        int timer;
        Color color;
        String type;

        public ShadeEffect(PkmnMap this$0, String type) {
            this(type, 30);
        }

        /* JADX INFO: 'super' call moved to the top of the method (can break code semantics) */
        public ShadeEffect(String type, int timer) {
            super(new Object[0]);
            PkmnMap.this = this$0;
            this.timer = 0;
            this.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            if (type.equals("graveyard")) {
                this.color = new Color(0.9f, 0.9f, 1.0f, 1.0f);
            } else if (type.equals("volcano")) {
                this.color = new Color(1.0f, 0.7f, 0.7f, 1.0f);
            } else if (type.equals("deep_forest")) {
                this.color = new Color(0.7f, 0.7f, 1.0f, 1.0f);
            }
            this.type = type;
            this.timer = timer;
        }

        @Override // com.corona.game.Action
        public void firstStep(Game game) {
            game.actionStack.remove(PkmnMap.this.shadeEffect);
            PkmnMap.this.shadeEffect = this;
        }

        @Override // com.corona.game.Action
        public void step(Game game) {
            this.timer--;
            if (this.timer < 0) {
                game.mapBatch.setColor(this.color);
                if (this.type.equals("graveyard")) {
                    FogEffect.type = FogEffect.Type.FOG;
                    FogEffect.refresh = true;
                } else if (this.type.equals("volcano")) {
                    FogEffect.type = FogEffect.Type.SMOKE;
                    FogEffect.refresh = true;
                } else if (this.type.equals("deep_forest")) {
                    FogEffect.type = FogEffect.Type.DEEPFOREST;
                    FogEffect.refresh = true;
                } else {
                    FogEffect.type = null;
                }
                game.actionStack.remove(this);
            }
        }
    }

    /* loaded from: pokewilds.jar:com/pkmngen/game/PkmnMap$PeriodicSave.class */
    public static class PeriodicSave extends Action {
        private float timeDelta = 60.0f;
        private float saveInterval = 60.0f;
        private static int framesSinceLastSave = 0;
        OutputStream outputStream;
        Output output;

        public static boolean isSaveOld() {
            return framesSinceLastSave >= 1200;
        }

        public static void resetTimer() {
            framesSinceLastSave = 0;
        }

        public static void increaseTimer() {
            framesSinceLastSave++;
        }

        public PeriodicSave(Game game) {
            super(new Object[0]);
        }

        @Override // com.corona.game.Action
        public Action.Layer getLayer() {
            return Action.Layer.map_500;
        }

        @Override // com.corona.game.Action
        public void step(Game game) {
            resetTimer();
            if (game.type != Game.Type.CLIENT) {
                this.timeDelta += Gdx.graphics.getDeltaTime();
                if (this.timeDelta >= this.saveInterval) {
                    this.timeDelta = 0.0f;
                    System.out.println("Backing up previous save file...");
                    try {
                        Files.copy(Paths.get(game.map.id + ".sav", new String[0]), Paths.get(game.map.id + ".sav.backup", new String[0]), StandardCopyOption.REPLACE_EXISTING);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Saving game to file...");
                    try {
                        this.outputStream = new DeflaterOutputStream(new FileOutputStream(game.map.id + ".sav"));
                        this.output = new Output(this.outputStream);
                    } catch (FileNotFoundException e2) {
                        e2.printStackTrace();
                    }
                    game.server.getKryo().writeObject(this.output, new Network.SaveData(game));
                    this.output.close();
                    System.out.println("Done.");
                    PixmapIO.writePNG(new FileHandle(game.map.id + ".png"), game.map.minimap);
                }
            }
        }
    }

    public void setView(Game game, OrthographicCamera camera) {
        float width = camera.viewportWidth * camera.zoom;
        float height = camera.viewportHeight * camera.zoom;
        this.viewBounds.set(camera.position.x - (width / 2.0f), camera.position.y - (height / 2.0f), width, height);
        if (!this.cacheBounds.contains(this.viewBounds) || this.refreshCache) {
            cacheOnscreenTiles(game);
        }
        if (this.refreshOnscreenPokemon) {
            cacheOnscreenPokemon(game);
        }
    }

    /* loaded from: pokewilds.jar:com/pkmngen/game/PkmnMap$YSort.class */
    public class YSort {
        public ArrayList<Tile> ySortTrees = new ArrayList<>();
        public ArrayList<Tile> ySortSavannaTrees = new ArrayList<>();
        public ArrayList<Pokemon> ySortPokemon = new ArrayList<>();
        int yPos;

        public YSort(int yPos) {
            PkmnMap.this = this$0;
            this.yPos = yPos;
        }
    }

    public void cacheOnscreenPokemon(Game game) {
        this.refreshOnscreenPokemon = false;
        System.out.println("Caching onscreen Pokemon");
        this.onscreenPokemon.clear();
        this.onscreenYsort1.clear();
        float layerTileWidth = (float) (16 * 1);
        float layerTileHeight = (float) (16 * 1);
        int col1 = ((int) (this.cacheBounds.x / layerTileWidth)) - 2;
        int col2 = ((int) (((this.cacheBounds.x + this.cacheBounds.width) + layerTileWidth) / layerTileWidth)) + 1;
        int row1 = ((int) (this.cacheBounds.y / layerTileHeight)) - 3;
        int row2 = ((int) (((this.cacheBounds.y + this.cacheBounds.height) + layerTileHeight) / layerTileHeight)) + 3;
        Vector2 position = new Vector2();
        for (int row = row2; row >= row1; row--) {
            YSort ySort = new YSort(row * 16);
            for (int col = col1; col < col2; col++) {
                position.set((float) (col * 16), (float) (row * 16));
                Pokemon pokemon = this.pokemon.get(position);
                if (pokemon != null) {
                    this.onscreenPokemon.add(pokemon);
                    ySort.ySortPokemon.add(pokemon);
                }
                Tile tile = this.tiles.get(position);
                if (!(tile == null || tile.overSprite == null)) {
                    if (tile.drawSavannaTree) {
                        ySort.ySortSavannaTrees.add(tile);
                    }
                    if (tile.drawAsTree) {
                        ySort.ySortTrees.add(tile);
                    }
                }
            }
            this.onscreenYsort1.add(ySort);
        }
    }

    public void cacheOnscreenTiles(Game game) {
        this.refreshCache = false;
        if (game.debugInputEnabled) {
            System.out.println("Caching onscreen tiles");
        }
        this.onscreenBelowBridgeTiles.clear();
        this.onscreenDrawAsTree.clear();
        this.onscreenEggs.clear();
        this.onscreenExteriorTidal.clear();
        this.onscreenFossils.clear();
        this.onscreenGrass.clear();
        this.onscreenOverSprites.clear();
        this.onscreenPokemon.clear();
        this.onscreenSavannaTrees.clear();
        this.onscreenShoreOcean.clear();
        this.onscreenShoreTidal.clear();
        this.onscreenSpriteProxies.clear();
        this.onscreenSprites.clear();
        this.onscreenTiles.clear();
        this.onscreenBurrowed.clear();
        this.onscreenYsort1.clear();
        this.cacheBounds.x = this.viewBounds.x - 16.0f;
        this.cacheBounds.y = this.viewBounds.y - 16.0f;
        this.cacheBounds.width = this.viewBounds.width + 32.0f;
        this.cacheBounds.height = this.viewBounds.height + 32.0f;
        float layerTileWidth = (float) (16 * 1);
        float layerTileHeight = (float) (16 * 1);
        int col1 = ((int) (this.cacheBounds.x / layerTileWidth)) - 2;
        int col2 = ((int) (((this.cacheBounds.x + this.cacheBounds.width) + layerTileWidth) / layerTileWidth)) + 1;
        int row1 = ((int) (this.cacheBounds.y / layerTileHeight)) - 3;
        int row2 = ((int) (((this.cacheBounds.y + this.cacheBounds.height) + layerTileHeight) / layerTileHeight)) + 3;
        Vector2 position = new Vector2();
        for (int row = row2; row >= row1; row--) {
            YSort ySort = new YSort(row * 16);
            for (int col = col1; col < col2; col++) {
                position.set((float) (col * 16), (float) (row * 16));
                Tile tile = this.tiles.get(position);
                Pokemon pokemon = this.pokemon.get(position);
                if (pokemon != null) {
                    this.onscreenPokemon.add(pokemon);
                    ySort.ySortPokemon.add(pokemon);
                    if (pokemon.isEgg && pokemon.mapTiles == this.tiles && (pokemon.standingAction instanceof Pokemon.Standing)) {
                        this.onscreenEggs.add(pokemon);
                    }
                }
                Pokemon pokemon2 = this.burrowedPokemon.get(position);
                if (pokemon2 != null) {
                    this.onscreenBurrowed.add(pokemon2);
                }
                if (tile != null) {
                    this.onscreenTiles.add(tile);
                    if (tile.sprite instanceof SmolSpriteProxy) {
                        this.onscreenSpriteProxies.add(tile);
                    } else {
                        this.onscreenSprites.add(tile);
                    }
                    if (tile.shoreOcean != null) {
                        this.onscreenShoreOcean.add(tile);
                    }
                    if (tile.shoreTidal != null) {
                        this.onscreenShoreTidal.add(tile);
                    }
                    if (tile.belowBridge) {
                        this.onscreenBelowBridgeTiles.add(tile);
                    }
                    if (tile.overSprite != null) {
                        if (!tile.isLedge) {
                            this.onscreenOverSprites.add(tile);
                        } else if (!tile.isWaterfall) {
                            this.onscreenOverSprites.add(tile);
                        }
                        if (tile.drawSavannaTree) {
                            this.onscreenSavannaTrees.add(tile);
                            ySort.ySortSavannaTrees.add(tile);
                        }
                        if (tile.isGrass && !tile.isTidal) {
                            this.onscreenGrass.add(tile);
                        }
                        if (tile.drawAsTree) {
                            this.onscreenDrawAsTree.add(tile);
                            ySort.ySortTrees.add(tile);
                        }
                    }
                    Tile exteriorTile = this.overworldTiles.get(position);
                    if (exteriorTile != null && exteriorTile.isTidal) {
                        this.onscreenExteriorTidal.add(tile);
                    }
                    if (tile.hasItem != null) {
                        this.onscreenFossils.add(tile);
                    }
                }
            }
            this.onscreenYsort1.add(ySort);
        }
    }

    /* loaded from: pokewilds.jar:com/pkmngen/game/PkmnMap$UpkeepTimers.class */
    public class UpkeepTimers extends Action {
        public Action.Layer layer = Action.Layer.map_130;
        int currIndex = 0;
        ArrayList<Pokemon>[] pokemonBuckets = new ArrayList[Pokemon.maxStagger];
        ArrayList<Pokemon> removeThese;

        /* JADX INFO: 'super' call moved to the top of the method (can break code semantics) */
        UpkeepTimers() {
            super(new Object[0]);
            PkmnMap.this = this$0;
            for (int i = 0; i < Pokemon.maxStagger; i++) {
                this.pokemonBuckets[i] = new ArrayList<>();
            }
            this.removeThese = new ArrayList<>();
        }

        @Override // com.corona.game.Action
        public Action.Layer getLayer() {
            return this.layer;
        }

        @Override // com.corona.game.Action
        public void firstStep(Game game) {
            this.currIndex = Pokemon.currStagger;
        }

        @Override // com.corona.game.Action
        public void step(Game game) {
            Iterator<Pokemon> it = this.pokemonBuckets[this.currIndex].iterator();
            while (it.hasNext()) {
                Pokemon pokemon = it.next();
                if (pokemon.drawLower == null) {
                    this.removeThese.add(pokemon);
                } else {
                    pokemon.drawLower.step(game);
                }
            }
            while (!this.removeThese.isEmpty()) {
                this.pokemonBuckets[this.currIndex].remove(this.removeThese.remove(0));
            }
            int i = this.currIndex + 1;
            this.currIndex = i;
            this.currIndex = i % Pokemon.maxStagger;
        }
    }
}
